﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.Services;

public partial class View_ManageBusStrips : System.Web.UI.Page
{
    private ManageBusTrip mnBusStrip;
    private ManageBusStaion mnBusStation;
    private ManageBusSchedule mnBusSchedule;
    private string SESSION_SEARCH = "SEARCH";

    /// <summary>
    /// ham goi khi bat dau load page
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        mnBusStrip = new ManageBusTrip();
        mnBusStation = new ManageBusStaion();
        mnBusSchedule = new ManageBusSchedule();
        if (!IsPostBack)
        {
            Session.Remove(SESSION_SEARCH);
        }
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(tbnExport);
    }

    protected void tbnExport_Click(object sender, EventArgs e)
    {
        rdgBusStrip.ExportSettings.Excel.Format = (GridExcelExportFormat)Enum.Parse(typeof(GridExcelExportFormat), "ExcelML");
        rdgBusStrip.ExportSettings.IgnorePaging = true;
        rdgBusStrip.ExportSettings.ExportOnlyData = true;
        rdgBusStrip.ExportSettings.OpenInNewWindow = true;
        rdgBusStrip.ExportSettings.FileName = "Danh-sach-tuyen-xe-" + DateTime.Now.ToString();
        rdgBusStrip.MasterTableView.ExportToExcel();
    }

    /// <summary>
    /// Xoa du lieu
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdgBusStrip_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        int pageIndex = rdgBusStrip.CurrentPageIndex;
        GridEditableItem editedItem = e.Item as GridEditableItem;
        BusStrip busStrip = new BusStrip();
        int id;
        busStrip.IDBusTrip = int.TryParse(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BusStripColumn.IDBusTrip].ToString(), out id) ? id : 0;

        int _result = mnBusStrip.deleteBusStrip(busStrip);
        if (_result == 1)
            Message.Show(rapBusStrip, "Xóa dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapBusStrip, "Xóa dữ liệu không thành công", Message.Type.Error);
        else
            Message.Show(rapBusStrip, "Xóa không thành công : Lỗi hệ thống", Message.Type.Error);
        rdgBusStrip.CurrentPageIndex = pageIndex;
    }

    /// <summary>
    /// ham them moi 1 tuyen bus
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdgBusStrip_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        BusStrip busStrip = new BusStrip();
        TextBox txtNameBusTrip = editedItem.FindControl("txtNameBusTrip") as TextBox;
        TextBox txtStartNameTrip = editedItem.FindControl("txtStartNameTrip") as TextBox;
        TextBox txtStopNameTrip = editedItem.FindControl("txtStopNameTrip") as TextBox;

        busStrip.NameBusTrip = txtNameBusTrip.Text.Trim();
        busStrip.StartNameTrip = txtStartNameTrip.Text.Trim();
        busStrip.StopNameTrip = txtStopNameTrip.Text.Trim();


        if (String.IsNullOrEmpty(busStrip.NameBusTrip) || String.IsNullOrEmpty(busStrip.StartNameTrip) || String.IsNullOrEmpty(busStrip.StopNameTrip))
        {
            Message.Show(rapBusStrip, "Chưa nhập dữ liệu", Message.Type.Error);
            return;
        }

        BusStation busStation = new BusStation();
        busStation.StaionName = txtStartNameTrip.Text.Trim();
        busStation.Latitude = latitude.Value.ToString();
        busStation.Longitude = longitude.Value.ToString();

        BusStation busStationStop = new BusStation();
        busStationStop.StaionName = txtStopNameTrip.Text.Trim();
        busStationStop.Latitude = latitudeStop.Value.ToString();
        busStationStop.Longitude = longitudeStop.Value.ToString();

        //them moi 1 diem dung vao bang,neu ton tai diem dung roi thi lay ra id diem dung do
        int resultAddBusStation = mnBusStation.insertNewBusStaion(busStation);
        int resultAddBusStationStop = mnBusStation.insertNewBusStaion(busStationStop);
        if (resultAddBusStation > 0 && resultAddBusStationStop >0)
        {
            //them moi 1 chuyen xe
            int _result = mnBusStrip.insertNewBusStrip(busStrip);
            if (_result >= 1)
            {
                BusSchedule schedule = new BusSchedule();
                schedule.IDBusTrip = _result;
                schedule.IDStation = resultAddBusStation;
                schedule.OrderSchedule = 1;
                schedule.Direction = "Chiều đi";
                int resultBusSchedule = mnBusSchedule.insertNewBusSchedule(schedule);

                BusSchedule scheduleStop = new BusSchedule();
                scheduleStop.IDBusTrip = _result;
                scheduleStop.IDStation = resultAddBusStationStop;
                scheduleStop.OrderSchedule = 1;
                scheduleStop.Direction = "Chiều về";
                int resultBusScheduleStop = mnBusSchedule.insertNewBusSchedule(scheduleStop);
                if (resultBusSchedule >= 0 && resultBusScheduleStop >= 0)
                {
                    Message.Show(rapBusStrip, "Thêm dữ liệu thành công!", Message.Type.Success);
                }
                else
                {
                    Message.Show(rapBusStrip, "Thêm không thành công : Lỗi hệ thống", Message.Type.Error);
                }
            }
            else if (_result == 0)
                Message.Show(rapBusStrip, "Thêm dữ liệu không thành công: đã tồn tại chuyến xe", Message.Type.Error);
            else
                Message.Show(rapBusStrip, "Thêm không thành công : Lỗi hệ thống", Message.Type.Error);
        }
        else
        {
            Message.Show(rapBusStrip, "Thêm không thành công : Lỗi hệ thống", Message.Type.Error);
            return;
        }
        rdgBusStrip.CurrentPageIndex = 0;
    }

    /// <summary>
    /// ham day du lieu vao radgrid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdgBusStrip_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        if ((Session[SESSION_SEARCH] != null) && (Session[SESSION_SEARCH].ToString() != string.Empty))
        {
            rdgBusStrip.DataSource = mnBusStrip.searchBusTripByName(Session[SESSION_SEARCH].ToString());
        }
        else
        {
            rdgBusStrip.DataSource = mnBusStrip.getAllBusTrip();
        }
    }
   
    /// <summary>
    /// ham chinh sua du lieu cua 1 hang
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdgBusStrip_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        int pageIndex = rdgBusStrip.CurrentPageIndex;
        GridEditableItem editedItem = e.Item as GridEditableItem;
        BusStrip busStrip = new BusStrip();

        int id;
        busStrip.IDBusTrip = int.TryParse(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BusStripColumn.IDBusTrip].ToString(), out id) ? id : 0;

        TextBox txtNameBusTrip = editedItem.FindControl("txtNameBusTrip") as TextBox;
        TextBox txtStartNameTrip = editedItem.FindControl("txtStartNameTrip") as TextBox;
        TextBox txtStopNameTrip = editedItem.FindControl("txtStopNameTrip") as TextBox;

        busStrip.NameBusTrip = txtNameBusTrip.Text.Trim();
        busStrip.StartNameTrip = txtStartNameTrip.Text.Trim();
        busStrip.StopNameTrip = txtStopNameTrip.Text.Trim();

        if (String.IsNullOrEmpty(busStrip.NameBusTrip) || String.IsNullOrEmpty(busStrip.StartNameTrip) || String.IsNullOrEmpty(busStrip.StopNameTrip))
        {
            Message.Show(rapBusStrip, "Chưa nhập dữ liệu", Message.Type.Error);
            return;
        }

        BusStation busStation = new BusStation();
        busStation.StaionName = txtStartNameTrip.Text.Trim();
        busStation.Latitude = latitude.Value.ToString();
        busStation.Longitude = longitude.Value.ToString();

        BusStation busStationStop = new BusStation();
        busStationStop.StaionName = txtStopNameTrip.Text.Trim();
        busStationStop.Latitude = latitudeStop.Value.ToString();
        busStationStop.Longitude = longitudeStop.Value.ToString();

        //them moi 1 diem dung vao bang,neu ton tai diem dung roi thi lay ra id diem dung do
        int resultAddBusStation = mnBusStation.insertNewBusStaion(busStation);
        int resultAddBusStationStop = mnBusStation.insertNewBusStaion(busStationStop);
        if (resultAddBusStation > 0 && resultAddBusStationStop > 0)
        {
            //mnBusSchedule.updateBusSchedule();
            int _result = mnBusStrip.updateBusStrip(busStrip);
            if (_result == 1)
                Message.Show(rapBusStrip, "Chỉnh sửa dữ liệu thành công!", Message.Type.Success);
            else if (_result == 0)
                Message.Show(rapBusStrip, "Chỉnh sửa dữ liệu không thành công", Message.Type.Error);
            else
                Message.Show(rapBusStrip, "Chỉnh sửa dữ liệu không thành công : Lỗi hệ thống", Message.Type.Error);
        }
        else
            Message.Show(rapBusStrip, "Chỉnh sửa dữ liệu không thành công : Lỗi hệ thống", Message.Type.Error);
        rdgBusStrip.CurrentPageIndex = pageIndex;
    }
   
    /// <summary>
    /// ham do du lieu vao tung hang cua radGrid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdgBusStrip_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            string s = ((DataRowView)e.Item.DataItem)["IDBusTrip"].ToString();
            int oder;
            oder = int.TryParse(s, out oder) ? oder : 0;
            ImageButton Edit = ((ImageButton)item["Edit"].Controls[0]);
            Edit.CssClass = "btnEditDelete";
            Edit.Width = 20;
            Edit.Height = 20;
            Edit.Attributes.Add("style", "background-color:transparent");
            ImageButton Delete = ((ImageButton)item["Delete"].Controls[0]);
            Delete.CssClass = "btnEditDelete";
            Delete.Width = 20;
            Delete.Height = 20;
            Delete.Attributes.Add("style", "background-color:transparent;");
            Edit.Visible = false;
            if (mnBusSchedule.getScheduleOrderMax("Chiều đi", oder) <= 1 && mnBusSchedule.getScheduleOrderMax("Chiều về", oder) <= 1)
            {
                Edit.Visible = true;
            }
        }
        if (e.Item.IsInEditMode)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
            {
                Button btnAdd = new Button();
                btnAdd = item.FindControl("UpdateButton") as Button;
                btnAdd.Text = "Thêm mới";
                btnAdd.CssClass = "myButton";
                btnAdd.Attributes.Add("style", "margin-left:0px !important;");
                TextBox txtNameBusTrip = item.FindControl("txtNameBusTrip") as TextBox;
                String clientId = txtNameBusTrip.ClientID.ToString();
                txtNameBusTriphdf.Value = clientId;
                TextBox txtStartNameTrip = item.FindControl("txtStartNameTrip") as TextBox;
                txtStartNameTriphdf.Value = txtStartNameTrip.ClientID.ToString();
                TextBox txtStopNameTrip = item.FindControl("txtStopNameTrip") as TextBox;
                txtStopNameTriphdf.Value = txtStopNameTrip.ClientID.ToString();
            }
            else
            {
                Button btnUpdate = new Button();
                btnUpdate = item.FindControl("UpdateButton") as Button;
                btnUpdate.Text = "Chỉnh sửa";
                btnUpdate.CssClass = "myButton";
                btnUpdate.Attributes.Add("style", "margin-left:0px !important;");
            }
            Button btnCancel = new Button();
            btnCancel = item.FindControl("CancelButton") as Button;
            btnCancel.Text = "Hủy bỏ";
            btnCancel.CssClass = "myButton";
            ImageButton hypelink = item.FindControl("HyperLinkMap") as ImageButton;
            hypelink.CssClass = "myHyperLink";
        }
    }

    /// <summary>
    /// ham lay du lieu cho viec tim kiem
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string stringSearch = txtKeyWord.Text.Trim();
        if (txtKeyWord.Text.Trim() == String.Empty)
        {
            stringSearch = hdfSearchString.Value.Trim();
        }

        if (Session[SESSION_SEARCH] != null)
        {
            if (hdfSearchString.Value.Trim() != Session[SESSION_SEARCH].ToString())
            {
                stringSearch = hdfSearchString.Value.Trim();
            }
            if (Session[SESSION_SEARCH].ToString() != txtKeyWord.Text.Trim())
            {
                stringSearch = txtKeyWord.Text.Trim();
            }
        }

        hdfSearchString.Value = stringSearch;
        rdgBusStrip.DataSource = mnBusStrip.searchBusTripByName(stringSearch);
        rdgBusStrip.Rebind();
        Session[SESSION_SEARCH] = stringSearch;
        txtKeyWord.Text = stringSearch;
    }
}